<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit" :showHistoryBtn="true">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>
<script>
	import {
		getFormFields,
		formModelLoad,
		formValidation,
		changeEditor,
		autoChangeEditor,
		configSelectData,
		formShowLoading,
		configFormEditor,
		hasEditor,
		getWorkCacheKey,
		setSubmitSubValue,
		changeConfigAttr
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js'
	import { confirmModal } from '@/static/js/common.js';
	import dateTimePicker from '@/static/js/dateTimePicker.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
export default {
	provide() {
		return {
			formPage: this
		};
	},
	data() {
		return {
			menuName: '公司年度培训计划表',
			busCode: 'HRS1102',
			formdata: {},
			mainConfigData: [],
			subConfigData: {},
			initValue: {},
			subEditorObj: {} ,//存放子表编辑器数据
			// instCacheKey:'hr-train-trainYearPlan-inst-cache',
			trainYearPlanId:""
		};
	},
	computed: {
		...mapState(useLoginUserStore, ['userName', 'userId'])
	},
	methods: {
		verifyTitle2(){
			let _that = this
			if(_that.$refs.autoform.hasEditor("title2","dataStr")&&!_that.$refs.autoform.formdata.year){
				let title  = _that.$refs.autoform.getEditorTitle("topinfo","year")
				_that.om.toast('请先选择'+title)
				return false
			}else{
				return true 
			}
		},
		// loadOtherData(instId) {
		// 	let _that = this;
		// 	var formdata = _that.$refs.autoform.formdata;
		// 	_that.om.request('tposition/getPrimary', { instId:instId, }).then(res => {
		// 		if (res.succeed) {
		// 			var value = res.value;
		// 			formdata.instPostId = value.id
		// 			formdata.instPostName = value.postName
		// 		} else {
		// 			_that.om.toast(res.msg);
		// 		}
		// 	});
		// },
		// clearInst(){
		// 	let _that = this;
		// 	_that.$refs.autoform.clear('title1', 'instId');
		// 	_that.$refs.autoform.clear('title1', 'parentInstId');
		// 	_that.$refs.autoform.clear('title1', 'instPostId');
		// },
		getWorkParam(){
			return {
				year:this.$refs.autoform.formdata.year,
				trainYearPlanId:this.trainYearPlanId
			}
		},
		onSelectZpos(){
			let _that = this
			let date = new Date()
			let yearList = [
				{
					name:Number(date.getFullYear()),
					value:Number(date.getFullYear())
				},{
					name:Number(date.getFullYear())+1,
					value:Number(date.getFullYear())+1
				}
			]
			_that.$refs.autoform.changeEditor("topinfo","year","data",configSelectData(yearList,"value","name"))
			_that.$refs.autoform.initSelectIndex("topinfo","year")
		},
		selectYear(item) {
			let _that = this;
			var editor = _that.subEditorObj.title2;
			confirmModal(
				_that,
				'提示信息',
				'是否加载该年度的部门年度培训需求审批表，使用已审批的培训信息？',
				function() {
					_that.om.request('trainYearPlan/findDataList',{year:item.name}).then(res => {
						if (res.succeed) {
							var val = res.value;
							if (val.unOverFlag) {
								confirmModal(_that,'提示信息','该年度有未审批的的部门年度培训需求审批表，使用已审批的培训信息？',()=>{
									editor.workList = val.value;
								},()=>{
									
								})
							} else {
								for(let item of val.value){
									delete item.trainInstDataList
								}
								editor.workList = val.value;
							}
						} else {
							_that.om.toast(res.msg);
						}
					});
				},
				function() {
					editor.workList = [];
				}
			);
			
		},
		formSubmit() {
			let _that = this;
			var checkRes = formValidation(this.$refs.autoform.configData, this.formdata);
			if (checkRes) {
				this.om.toast(checkRes);
			} else {
				//子表提交数据赋值
				setSubmitSubValue();
				if (!_that.formdata.dataStr) {
					this.om.toast(this.subEditorObj['title2'].title + '不能为空');
					return;
				}
				if(_that.edit){
					_that.om.formSave('trainYearPlan/doUpdate', _that.formdata, _that);
				}else{
					_that.om.formSave('trainYearPlan/doAdd', _that.formdata, _that);
				}
			}
		},
		initLoad(options) {
			let _that = this;
			let ajaxArr = [
				urlUtil.zposUrl(options.code)
			]
			let fileId = options.fileId
			if(fileId){
				_that.trainYearPlanId = fileId
				ajaxArr.push({
					url:"trainYearPlan/getValue",
					param:{
						fileId:fileId
					}
				})
			}
			//多个AJAX全部完成时开始执行
			formModelLoad(ajaxArr,function(res,mainData){
				changeEditor("topinfo","zposId","data",configSelectData(res[0],"id","posname"),mainData)
				changeEditor(mainData[0].editors, 'zuserId', {
					defaultValue: _that.userId,
					defaultName: _that.userName
				});
				//培训相关信息课程信息
				changeConfigAttr('title2', 'title', _that.subEditorObj['title2'].title, mainData);
				changeEditor(
					'title2',
					'dataStr',
					{
						title: _that.subEditorObj['title2'].title.substr(1),
						getWorkParam:"getWorkParam",
						titleTemplate: '培训项目名称: <%= projectName %>',
						subtitle1Template: '<%= startDateObjName %> ~ <%= endDateObjName %>',
						subtitle2Template: '培训方式: <%= trainTypeName %>'
					},
					null,
					mainData
				);
				if(fileId){
					let fieldArr = getFormFields(mainData)
					fieldArr.push("id")
					for(let index in res[1].value){
						if(fieldArr.includes(index)){
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[1].value[index])
							_that.$set(_that.formdata,index,res[1].value[index])
						}
					}
					let dataList = _that._.cloneDeep(res[1].value.dataList)
					_that.subEditorObj['title2'].workList = dataList
					_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"dataStr",JSON.stringify(dataList))
					_that.$set(_that.formdata,"dataStr",JSON.stringify(dataList))
					delete res[1].value.dataList
					
					_that.$nextTick(function(){
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.initSelectVal("topinfo","zposId")
						}else{
							_that.$refs.autoform.initSelectIndex("topinfo","zposId")
						}
						_that.onSelectZpos()
					})
				}
			},options)
		}
	},
	onLoad(options) {
		let _that = this;
		_that.menuName = options.title || '公司年度培训计划表';
		uni.setNavigationBarTitle({
			title: _that.menuName
		});
		formShowLoading(_that);
		_that.busCode = options.code || 'HRS1110';
		_that.formdata.userId = this.userId;
		_that.initLoad(options);
	},
	onShow() {
		let _that = this;
		if (_that.$refs.autoform) {
			for (var fieldId in _that.subEditorObj) {
				var editor = _that.subEditorObj[fieldId];
				uni.getStorage({
					key: getWorkCacheKey(fieldId),
					success: function(res) {
						editor.workList = res.data;
						uni.removeStorage({
							key: getWorkCacheKey(fieldId)
						});
					}
				});
			}
			// uni.getStorage({
			// 	key: _that.instCacheKey,
			// 	success: function(res) {
			// 		if (res.data) {
			// 			let d = res.data;
			// 			var formdata = _that.$refs.autoform.formdata;
			// 			formdata.instId = d.id;
			// 			formdata.instName = d.instname;
			// 			formdata.parentInstId = d.parentinstId;
			// 			formdata.parentInstName = d.parentinstName;
			// 			_that.loadOtherData(d.id);
			// 			uni.removeStorage({
			// 				key: _that.instCacheKey
			// 			});
			// 		}
			// 	}
			// });
		}
	}
};
</script>
<style></style>
